package com.hidtechs.popfax.controller;

import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.hidtechs.popfax.xml.XMLContactsHandler;
import com.hidtechs.popfax.xml.XMLDeleteHandler;
import com.hidtechs.popfax.xml.XMLInboxHandler;
import com.hidtechs.popfax.xml.XMLLoginHandler;
import com.hidtechs.popfax.xml.XMLOutboxHandler;

public class Requestor {

	
	
	public ArrayList<Object> requestData(Character class_type, final HashMap<String,String> attributes) {

		try{
			
			URL siteUrl= new URL(Constants.WEBSERVER_URL);
			HttpURLConnection conn= (HttpURLConnection)siteUrl.openConnection();
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			DataOutputStream dataOutputStream=new DataOutputStream(conn.getOutputStream());

			Set keys=attributes.keySet();
			Iterator iter= keys.iterator();
			String content="";
			for(int m=0;iter.hasNext();m++){
				Object key=iter.next();
				if(m!=0)
				{
					content +="&";
				}
				content +=key+"="+URLEncoder.encode(attributes.get(key),"UTF-8");
			}
			
			dataOutputStream.writeBytes(content);
			dataOutputStream.flush();
			return XMLImplementer(class_type,conn.getInputStream());

		}catch(Exception e){

			e.printStackTrace();
			return null;
		}
	}

	public ArrayList<Object> XMLImplementer(Character class_type,InputStream dataStreamFromServer)
	{
		try{
			ArrayList<Object> arrayListReturn=null;
			
			/* Get a SAXParser from the SAXPArserFactory. */ 
			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 
			SAXParser saxParser = saxParserFactory.newSAXParser(); 
	
			/* Get the XMLReader of the SAXParser we created. */ 
			XMLReader xmlReader = saxParser.getXMLReader(); 
			/* Create a new ContentHandler and apply it to the XML-Reader*/ 
			
			switch(class_type)
			{
			case 'l' : XMLLoginHandler xLoginHandler = new XMLLoginHandler();
						xmlReader.setContentHandler(xLoginHandler);
						xmlReader.parse(new InputSource(dataStreamFromServer));
						arrayListReturn=xLoginHandler.arrayOfObject;
						break;
						
			case 'i' : XMLInboxHandler xInboxHandler = new XMLInboxHandler();
						xmlReader.setContentHandler(xInboxHandler);
						xmlReader.parse(new InputSource(dataStreamFromServer));
						arrayListReturn= xInboxHandler.arrayOfObject;
						break;

			case 'o' : XMLOutboxHandler xOutboxHandler = new XMLOutboxHandler();
						xmlReader.setContentHandler(xOutboxHandler);
						xmlReader.parse(new InputSource(dataStreamFromServer));
						arrayListReturn= xOutboxHandler.arrayOfObject;
						break;
			case 'c' : XMLContactsHandler xContactsHandler = new XMLContactsHandler();
						xmlReader.setContentHandler(xContactsHandler);
						xmlReader.parse(new InputSource(dataStreamFromServer));
						arrayListReturn= xContactsHandler.arrayOfObject;
						break;
			case 'd' : XMLDeleteHandler xFaxDeleteHandler = new XMLDeleteHandler();
						xmlReader.setContentHandler(xFaxDeleteHandler);
						xmlReader.parse(new InputSource(dataStreamFromServer));
						arrayListReturn=xFaxDeleteHandler.arrayOfObject;
						break;
			case 'e' : XMLDeleteHandler xContactDeleteHandler = new XMLDeleteHandler();
						xmlReader.setContentHandler(xContactDeleteHandler);
						xmlReader.parse(new InputSource(dataStreamFromServer));
						arrayListReturn=xContactDeleteHandler.arrayOfObject;
						break;
			case 'f' : 	XMLDeleteHandler xContactEditHandler = new XMLDeleteHandler();
						xmlReader.setContentHandler(xContactEditHandler);
						xmlReader.parse(new InputSource(dataStreamFromServer));
						arrayListReturn=xContactEditHandler.arrayOfObject;
						break;
		
			}
			 
			
			return arrayListReturn;
			
		} catch(Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	}

